package 实体;
import java.sql.*;

public class dbutils {
    private static String driver,url,username,password;
    private static Connection con;

    public dbutils(){
        driver="com.mysql.cj.jdbc.Driver";
        url="jdbc:mysql://localhost:3306/swim_system?characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        username="root";
        password="admin";
    }

    public static Connection getCon() throws SQLException{
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("数据库驱动加载成功！");
        }catch (ClassNotFoundException e){
            System.out.println("数据库驱动加载失败！");
            e.printStackTrace();
        }
        try
        {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/swim_system?characterEncoding=utf8&serverTimezone=Asia/Shanghai" , "root" , "admin" ) ;
            System.out.println("数据库连接成功！");
        }
        catch(SQLException se)
        {
            System.out.println("数据库连接失败！");
            se.printStackTrace() ;
        }
        return con;
    }

    public static void close(Connection conn,Statement stat,ResultSet rs) throws SQLException {
        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw e;
            }
        }
        if(stat != null) {
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw e;
            }
        }
        if(conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw e;
            }
        }
    }
    public static void main(String[] args) throws SQLException{
        dbutils db=new dbutils();
        db.getCon();
    }
}

